package com.ruoyi.common.utils.zfb;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayFundTransUniTransferModel;
import com.alipay.api.domain.Participant;
import com.alipay.api.request.AlipayFundTransUniTransferRequest;
import com.alipay.api.response.AlipayFundTransUniTransferResponse;
import com.ruoyi.common.core.domain.DTO.WapTransferBO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.DecimalFormat;
@Slf4j
@Component
public class AlipayFundTransUniTransferWap {

    @Value("${alipay.biz-scene}")
    private String bizScene;

    @Value("${alipay.product-code}")
    private String productCode;

    @Value("${alipay.identity-type}")
    private String identityType;

    @Value("${alipay.wap.app-id}")
    private String appId;

    @Value("${alipay.wap.private-key}")
    private String privateKey;

    @Value("${alipay.wap.server-url}")
    private String serverUrl;

    @Value("${alipay.wap.alipay-cert-public-key}")
    private String alipayCertPublicKey;

    @Value("${alipay.wap.app-cert-public-key}")
    private String appCertPublicKey;

    @Value("${alipay.wap.alipay-root-cert}")
    private String rootCertPath;

    public AlipayFundTransUniTransferResponse wapTransfer(WapTransferBO wapTransferBO) throws AlipayApiException {


        // 初始化SDK
        AlipayClient alipayClient = new DefaultAlipayClient(getAlipayConfig(privateKey, appId, serverUrl, alipayCertPublicKey, appCertPublicKey, rootCertPath));

        // 构造请求参数以调用接口
        AlipayFundTransUniTransferRequest request = new AlipayFundTransUniTransferRequest();
        AlipayFundTransUniTransferModel model = new AlipayFundTransUniTransferModel();

        // 设置商家侧唯一订单号
        model.setOutBizNo(wapTransferBO.getOrderNo());

        BigDecimal transferAmount = wapTransferBO.getTransferAmount();
        DecimalFormat df = new DecimalFormat("0.00");
        String transferAmountStr = df.format(transferAmount);
        // 设置订单总金额
        model.setTransAmount(transferAmountStr);

        // 设置描述特定的业务场景
        model.setBizScene(bizScene);

        // 设置业务产品码
        model.setProductCode(productCode);

        // 设置转账业务的标题
        model.setOrderTitle("奖励发放");

        // 设置收款方信息
        Participant payeeInfo = new Participant();
        payeeInfo.setIdentity(wapTransferBO.getAlipayLoginName());
        payeeInfo.setName(wapTransferBO.getName());
        payeeInfo.setIdentityType(identityType);
        model.setPayeeInfo(payeeInfo);

        // 设置转账业务请求的扩展参数
//        model.setBusinessParams("{\"payer_show_name_use_alias\":\"true\"}");

        request.setBizModel(model);
        AlipayFundTransUniTransferResponse response = alipayClient.certificateExecute(request);
        log.info("支付宝转账返回结果:{}", response.getBody());

        if (response.isSuccess()) {
            log.info("支付宝转账成功");
        } else {
            log.info("支付宝转账失败");
            // sdk版本是"4.38.0.ALL"及以上,可以参考下面的示例获取诊断链接
            // String diagnosisUrl = DiagnosisUtils.getDiagnosisUrl(response);
            // System.out.println(diagnosisUrl);
        }
        return response;
    }

    private static AlipayConfig getAlipayConfig(String privateKey, String appId, String serverUrl, String alipayCertPublicKey, String appCertPublicKey, String rootCertPath) {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setServerUrl(serverUrl);
        alipayConfig.setAppId(appId);
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        alipayConfig.setFormat("json");
        alipayConfig.setAppCertPath(appCertPublicKey);
        alipayConfig.setAlipayPublicCertPath(alipayCertPublicKey);
        alipayConfig.setRootCertPath(rootCertPath);
        return alipayConfig;
    }
}